package controller;

import java.io.File;
import java.util.List;

import map.GameMap;
import map.GameWorld;
import model.Observer;
import model.Subject;

public class AudioController extends Controller implements Observer {
	
	GameWorld mWorld;
	GameMap mCurrentMap;
	
	public AudioController() {
		mWorld = GameWorld.getInstance();
		
		mWorld.register(this);
	}

	@Override
	public void update(Subject sourceSubject) {
		if (sourceSubject.equals(mWorld)) {
			// update noification from world, see if map has changed
			GameMap map = mWorld.getCurrentMap();
			
			if (map == null) return;
			
			if (! map.equals(mCurrentMap)) {
				// map has changed, change music
				playMapMusic(map);
				mCurrentMap = map;
			}
		}
		
	}
	
	private void playMapMusic(GameMap map) {
		File soundFile = new File(String.format("res/audio/map/%s.mid", map.getMapName()));
		
		if (! soundFile.exists()) {
			return;
		}
		
		// otherwise, play it
		MidiPlayer.getInstance().playMidi(soundFile);
	}

	@Override
	public List<GameControl> getKeyControls() {
		// TODO Auto-generated method stub
		return null;
	}

	
	public void reset() {
		MidiPlayer.getInstance().stop();
	}
}
